package tp.pr5.commands;

import java.util.StringTokenizer;

import tp.pr5.Game;
import tp.pr5.Map;
import tp.pr5.MensajesConst;
import tp.pr5.Player;
import tp.pr5.Room;
import tp.pr5.commands.exceptions.CommandExecutionException;
import tp.pr5.commands.exceptions.WrongCommandFormatException;
import tp.pr5.items.Item;

public class UseCommand implements Command
{

	private Player _friki;
	private Game _game;
	private String _itemToBeUsedId;
	@SuppressWarnings("unused")
	private Room _habitacion;

	@Override public void execute() throws CommandExecutionException
	{

		Item item = this._friki.getItem(this._itemToBeUsedId);
		if (item == null)
			throw new CommandExecutionException(MensajesConst.stoledItem+ this._itemToBeUsedId + ".");

		//if(item.getId().equalsIgnoreCase("key"))			this._friki.requestPlayerUpdate();
		
		if(!item.use(this._friki, this._game.getCurrentRoom()))
			throw new CommandExecutionException("I did not go to TP classes last week, I do not know how to use it...\n");
		
		//este es el if superxungo
		if(!this._itemToBeUsedId.equalsIgnoreCase("llave") && !this._itemToBeUsedId.equalsIgnoreCase("moneda"))
			this._friki.requestPlayerUpdate();
		
		this._friki.requestInventoryUpdate();		

	}
	public void configureContext(Game g, Map m, Player p) {
		this._friki=p;
		this._habitacion=m.getCurrentRoom();
		this._game=g;
	}
	@Override public String getHelp()
	{
		return MensajesConst.hUse;
	}

	@Override public Command parse(String cad)
			throws WrongCommandFormatException
	{
		StringTokenizer tkn = new StringTokenizer(cad);
		if (tkn.countTokens() != 2)
			throw new WrongCommandFormatException();
		String verb = tkn.nextToken();

		if (!(verb.toUpperCase().equalsIgnoreCase("USE") || verb.toUpperCase().equalsIgnoreCase("USAR")))
			throw new WrongCommandFormatException();
		//this._game = execContext;
		//this._friki = this._game.getPlayer();
		this._itemToBeUsedId = tkn.nextToken();
		return this;

	}

}
